﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

using Matchingo.Common.Types.Enumerations;

namespace Matchingo.Modules.TitleScreenModule.Views
{
    public partial class TitleScreenView : UserControl, ITitleScreenView
    {
        #region ========== Local Members ==========

        private const string MENU_SLIDE_IN_LEFT_STORYBOARD = "SlideInLeftStoryboard";
        private const string MENU_SLIDE_OUT_LEFT_STORYBOARD = "SlideOutLeftStoryboard";
        private const string MENU_SLIDE_IN_RIGHT_STORYBOARD = "SlideInRightStoryboard";
        private const string MENU_SLIDE_OUT_RIGHT_STORYBOARD = "SlideOutRightStoryboard";
        private const string MENU_SLIDE_IN_UP_STORYBOARD = "SlideInUpStoryboard";
        private const string MENU_SLIDE_OUT_UP_STORYBOARD = "SlideOutUpStoryboard";

        private TitleScreenViewModel ViewModel { get; set; }

        public UIElement RootElement { get { return LayoutRoot; } }

        #endregion

        #region ========== Constructors ==========

        public TitleScreenView()
        {            
            InitializeComponent();            
        }

        #endregion

        #region ========== Public Methods ==========

        public void Initialize(TitleScreenViewModel model)
        {
            ViewModel = model;
            DataContext = ViewModel;

            TitleGridButton.MouseLeftButtonUp += TitleGridButtonMouseLeftButtonUp;
        }

        public void ChangeTitleArrowDirection(DirectionNames.Direction dir)
        {
            MenuButtonLeft.ArrowDirection = dir;
            MenuButtonRight.ArrowDirection = dir;
        }

        public void ShowScreen(DirectionNames.Direction direction, bool skipAnimation)
        {
            switch (direction)
            {
                case DirectionNames.Direction.Left:
                    PlayStoryboard(MENU_SLIDE_IN_LEFT_STORYBOARD, skipAnimation);
                    return;
                case DirectionNames.Direction.Right:
                    PlayStoryboard(MENU_SLIDE_IN_RIGHT_STORYBOARD, skipAnimation);
                    return;
                case DirectionNames.Direction.Up:
                    PlayStoryboard(MENU_SLIDE_IN_UP_STORYBOARD, skipAnimation);
                    return;
            }
        }

        public void HideScreen(DirectionNames.Direction direction, bool skipAnimation)
        {

            switch (direction)
            {
                case DirectionNames.Direction.Left:
                    PlayStoryboard(MENU_SLIDE_OUT_LEFT_STORYBOARD, skipAnimation);
                    return;
                case DirectionNames.Direction.Right:
                    PlayStoryboard(MENU_SLIDE_OUT_RIGHT_STORYBOARD, skipAnimation);
                    return;
                case DirectionNames.Direction.Up:
                    PlayStoryboard(MENU_SLIDE_OUT_UP_STORYBOARD, skipAnimation);
                    return;
            }
        }

        #endregion

        #region ========== Private Methods ==========

        private bool TransitionMenu(InOutEnumeration.InOut inOrOut, DirectionNames.Direction direction, bool skipAnimation)
        {
            switch (direction)
            {
                case DirectionNames.Direction.Left:
                    PlayStoryboard(inOrOut == InOutEnumeration.InOut.In ?
                        MENU_SLIDE_IN_LEFT_STORYBOARD : MENU_SLIDE_OUT_LEFT_STORYBOARD, skipAnimation);
                    return true;
                case DirectionNames.Direction.Right:
                    PlayStoryboard(inOrOut == InOutEnumeration.InOut.In ?
                        MENU_SLIDE_IN_RIGHT_STORYBOARD : MENU_SLIDE_OUT_RIGHT_STORYBOARD, skipAnimation);
                    return true;
                case DirectionNames.Direction.Up:
                    PlayStoryboard(inOrOut == InOutEnumeration.InOut.In ?
                        MENU_SLIDE_IN_UP_STORYBOARD : MENU_SLIDE_OUT_UP_STORYBOARD, skipAnimation);
                    return true;
            }
            return false;
        }

        // TODO:: this is duplicated across all views in modules
        private void PlayStoryboard(string storyBoardName, bool skipAnimation)
        {
            Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
            {
                Storyboard sb = (Storyboard)this.Resources[storyBoardName];
                sb.Begin();
                if (skipAnimation)
                {
                    sb.SkipToFill();
                }
            });
        }

        #endregion

        #region ========== Event Handlers ==========

        void TitleGridButtonMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ViewModel.ToggleMenu();
        }

        private void About_Clicked(object sender, RoutedEventArgs e)
        {
            ViewModel.ShowAboutScreen();
        }

        private void Options_Clicked(object sender, RoutedEventArgs e)
        {
            ViewModel.ShowOptions();
        }

        private void HighScores_Clicked(object sender, RoutedEventArgs e)
        {
            ViewModel.ShowHighScores();
        }

        private void NewGame_Clicked(object sender, RoutedEventArgs e)
        {
            ViewModel.StartNewGame(PlayerNameTextbox.Text);
        }

        #endregion
    }
}
